package com.qf.edu.controller;

import com.qf.edu.service.DrugsLimitService;
import com.qf.edu.service.impl.DrugsLimitServiceImpl;
import com.qf.edu.utils.PageUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * @author:huayuzhuo
 * @email:2798866329@qq.com
 * @desc:药品库存分页查询控制台
 * @datatime: 2022/6/7 16:25
 */
@WebServlet("/nurse/drugsLimit")
public class DrugsLimit extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        // 获取数据  当前的页码
        int index = Integer.parseInt(req.getParameter("index"));
        int pageSize = 5;
        //实例化一个分页类
        PageUtils pageUtils = new PageUtils();
        pageUtils.setCurrentIndex(index);
        pageUtils.setPageSize(pageSize);

        DrugsLimitService drugsLimitService = new DrugsLimitServiceImpl();
        Map<String, Object> map = drugsLimitService.drugsMan(pageUtils);
        //从map中获取药品数量赋给分页类   并转int类型
        int drugsAll = Integer.parseInt(map.get("drugsAll").toString());
        pageUtils.setTotalCount(drugsAll);

        //总页数
        if (drugsAll % pageSize == 0){
            pageUtils.setTotalPages(drugsAll/pageSize);
        }  else {
            pageUtils.setTotalPages(drugsAll/pageSize + 1);
        }

        // 获取session对象  将赋值好的分页类和要分页的数据全传入session
        HttpSession session = req.getSession();
        session.setAttribute("PageUtils",pageUtils);
        session.setAttribute("list",map.get("list"));
        resp.sendRedirect("getAllDrugs.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
